export default function polyfillHelper() {
  if (!document.querySelectorAll) {
    document.querySelectorAll = function (selectors) {
      let style = document.createElement('style'), elements = [], element;
      document.documentElement.firstChild.appendChild(style);
      document._qsa = [];

      style.styleSheet.cssText = selectors + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
      window.scrollBy(0, 0);
      style.parentNode.removeChild(style);

      while (document._qsa.length) {
        element = document._qsa.shift();
        element.style.removeAttribute('x-qsa');
        elements.push(element);
      }
      document._qsa = null;
      return elements;
    };
  }

  if (!document.querySelector) {
    document.querySelector = function (selectors) {
      var elements = document.querySelectorAll(selectors);
      return (elements.length) ? elements[0] : null;
    };
  }

  // 用于在IE6和IE7浏览器中，支持Element.querySelectorAll方法
  var qsaWorker = (function () {
    var idAllocator = 10000;

    function qsaWorkerShim(element, selector) {
      var needsID = element.id === "";
      if (needsID) {
        ++idAllocator;
        element.id = "__qsa" + idAllocator;
      }
      try {
        return document.querySelectorAll("#" + element.id + " " + selector);
      } finally {
        if (needsID) {
          element.id = "";
        }
      }
    }

    function qsaWorkerWrap(element, selector) {
      return element.querySelectorAll(selector);
    }

    // Return the one this browser wants to use
    return document.createElement('div').querySelectorAll ? qsaWorkerWrap : qsaWorkerShim;
  })();

  if (!document.prepend) {
    Document.prototype.prepend = HTMLElement.prototype.prepend = function (nodes) {
      if (this.hasChildNodes()) {
        this.insertBefore(nodes, this.firstChild);
      } else {
        this.appendChild(nodes);
      }
      return this;
    }
  }
}
